package com.lzl.logdesenshelper.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.util.Assert;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;


public class AspectMethodHelper {

    private AspectMethodHelper() {
    }

    public static Map<String, Object> getMappingOfParameters(JoinPoint point) {
        return getMappingOfParameters(getMethodParameterNames(point), point.getArgs());
    }

    public static Map<String, Object> getMappingOfParameters(String[] keys, Object[] values) {
        Assert.isTrue(keys.length == values.length, "The parameters does not match the values.");
        Map<String, Object> result = new LinkedHashMap<>();
        for (int i = 0; i < keys.length; i++) {
            result.put(keys[i], values[i]);
        }
        return result;
    }

    public static String[] getMethodParameterNames(JoinPoint point) {
        return getMethodSignature(point).getParameterNames();
    }

    public static Method getMethod(JoinPoint point) {
        return getMethodSignature(point).getMethod();
    }

    public static MethodSignature getMethodSignature(JoinPoint point) {
        return (MethodSignature) point.getSignature();
    }
}
